#include "include/CertTestWindow.h"

CertTestWindow::CertTestWindow()
    :InnerWindow("cert")
{
    this->setupUi(this);

    pTcpSocket = new QTcpSocket(this);
    connect(pTcpSocket, SIGNAL(connected()), this, SLOT(connected()));
    connect(pTcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
    connect(pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)));
    connect(pTcpSocket, SIGNAL(readyRead()), this, SLOT(readyToRead()));
    connect(this->pushButton, SIGNAL(clicked()), this, SLOT(dwnPressed()));

    progressBar->setMaximum(0);
    progressBar->setMinimum(0);
    progressBar->hide();
}

CertTestWindow::~CertTestWindow()
{
    if(pTcpSocket->isOpen())
        pTcpSocket->disconnect();
}

void CertTestWindow::clearInnerWindow()
{
}

void CertTestWindow::connected()
{
    qDebug()<<"connected();";
    pTcpSocket->write("<wlbs_handshake><get_certificate/></wlbs_handshake>");
}

void CertTestWindow::disconnected()
{
}

void CertTestWindow::readyToRead()
{
    qDebug()<<"readyToRead();";
    qDebug()<<pTcpSocket->bytesAvailable();
    QByteArray bf = pTcpSocket->readAll();
    qDebug()<<bf;
    this->textBrowser->setText(bf);

    this->progressBar->hide();
    this->pushButton->setText("Accept cert");
    this->pushButton->show();
}

void CertTestWindow::error(QAbstractSocket::SocketError err)
{
    qDebug()<<"err: "<<err;
}

void CertTestWindow::sendToServer()
{
}

void CertTestWindow::dwnPressed()
{
    if(this->pushButton->text() == "Accept cert")
        return;

    pTcpSocket->connectToHost("82.144.223.145", 7777);
    pushButton->hide();
    progressBar->show();
}
